package leetcode.primary;

public class T287 {
    public int findDuplicate(int[] nums) {
        if (nums.length==1 || nums[1]==nums[0]){
            return nums[0];
        }
        int temp1 = nums[0]^nums[1];

        int i =2;
        for (; i < nums.length; i++) {
            int temp2 = nums[i]^temp1;
            System.out.print(temp1+" ");
            if ((temp2^temp1) ==nums[i]){
                return nums[i];
            }
            temp1 = temp2;
        }
        return 0;
    }

    public static void main(String[] args) {
        T287 t287 = new T287();
        int i = t287.findDuplicate(new int[]{1, 3, 4, 2, 2});
        System.out.println();
        System.out.println(i);
    }
}
